package scct.zhang.dao;

import scct.zhang.model.GoodsType;
import scct.zhang.model.User;
import scct.zhang.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDao {
    public List<GoodsType> getAllType(){
        List<GoodsType> list = new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_goodstype";
//        PreparedStatement pstmt = null;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String remark = rs.getString("remark");
                list.add(new GoodsType(id,name,remark));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public boolean addType(String name,String remark){
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String insert_sql = "insert into sm_goodstype values(null,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(insert_sql);
            pstmt.setString(1,name);
            pstmt.setString(2,remark);
            result = pstmt.executeUpdate();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return result> 0;
    }

    public boolean updateType(int id, String name, String remark){
        int count = 0;
        Connection conn = DBUtils.getConnection();
        String update_sql = "update sm_goodstype set remark=? where id = ?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(1,remark);
            count = pstmt.executeUpdate();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return count>0;
    }

    public boolean isDumptype(String name){
        int count = 0;// 查到的用户数量
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_goodstype where name=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                count =1;
            }
//            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count>0;
    }

    public GoodsType getTypeById(int id){
        GoodsType goodsType = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_goodstype where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);

            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                String name = rs.getString("name");
                String remark = rs.getString("remark");

                goodsType = new GoodsType(id,name,remark);

            }
            DBUtils.closeConnection(conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return goodsType;
    }

}
